package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.jyy.Shedudetail;
import com.sdry.model.llm.TeamRecord;
import com.sdry.service.llm.TeamRecordService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 班组记录
* @author lian 
* @version 1.0
* @time 2018年10月17日 下午3:36:36
*/

@Controller
@RequestMapping("/teamRecordMana")
public class TeamRecordController {

	@Resource
	private TeamRecordService teamRecordService;  //注入班组记录service
	
	/**
	 * 分页查询班组记录
	 * @param page 分页条件
	 * @param teamRecord 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageTeamRecord")
	public void listPageTeamRecord(Page page, TeamRecord teamRecord, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (teamRecord.getTname() != null ) {
			map.put("tname", teamRecord.getTname());
		} else {
			map.put("tname", "");
			teamRecord.setTname("");
		}
		int count = teamRecordService.countTeamRecord(teamRecord);
		List<TeamRecord> list = teamRecordService.listPageTeamRecord(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 分页查询班组长选择的排产单
	 * @param page 分页条件
	 * @param teamRecord 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/selecDetailtList")
	public void selecDetailtList(Page page, TeamRecord teamRecord, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != teamRecord.getProductionId()) {
			map.put("productionId", teamRecord.getProductionId());
		} else {
			map.put("productionId", "");
			teamRecord.setProductionId("");
		}
		int count = teamRecordService.countShedudetail(teamRecord);
		List<Shedudetail> list = teamRecordService.listPageShedudetail(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存班组记录
	 * @param teamRecord 班组记录实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveTeamRecord")
	public void saveTeamRecord(TeamRecord teamRecord, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = 0;
		isSuccess = teamRecordService.insertTeamRecord(teamRecord, session);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 修改已选排产单
	 * @param teamRecord 班组记录实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/updateTeamRecordOnPositionId")
	public void updateTeamRecordOnPositionId(TeamRecord teamRecord, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = 0;
		isSuccess = teamRecordService.updateTeamRecordOnPositionId(teamRecord, session);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 查询当天班组记录
	 * @param teamRecord 班组记录实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getTeamRecordByTime")
	public void getTeamRecordByTime(TeamRecord teamRecord, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = 0;
		isSuccess = teamRecordService.getTeamRecordByTime(teamRecord, session);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除班组记录
	 * @param str tid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTeamRecord")
	public void deleteTeamRecord(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = teamRecordService.deleteTeamRecord(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除删除班组记录
	 * @param str tid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTeamRecordOfVerify")
	public void deleteTeamRecordOfVerify(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = teamRecordService.deleteTeamRecordOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除班组长选定的排产单
	 * @param pid 排产单ID
	 * @param tid 班组记录ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTeamRecordOfPid")
	public void deleteTeamRecordOfPid(@RequestParam("teamRecordId") Long tid, @RequestParam("shedudetailId") Long pid, HttpServletResponse response) throws Exception{
		int isSuccess = teamRecordService.deleteTeamRecordOfPid(tid, pid);
		ResponseUtil.write(response, isSuccess);
	}
}
